package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.ModuloEntrega;

public class ModuloEntregaDAOImpl extends JdbcDaoSupport implements IModuloEntregaDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;
    
    private ConsultaModulosQuery consultaModulosQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM GES_MODULO_ENTREGA_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(ModuloEntrega moduloEntrega) {
		deleteOperation.execute( moduloEntrega  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public ModuloEntrega findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(ModuloEntrega moduloEntrega) {
		insertOperation.execute( moduloEntrega );
	}

	public void update(ModuloEntrega moduloEntrega) {
		updateOperation.execute( moduloEntrega );
	}

	public ModuloEntrega findByCriteria(ModuloEntrega  moduloEntrega) {
		return findByCriteriaQuery.runQuery(moduloEntrega);
	}

	public List load(ModuloEntrega moduloEntrega ) {
		return findAllQuery.runQuery(moduloEntrega);
	}
	
	public List consultaModulos() {
		return consultaModulosQuery.runQuery();
	}

	public ModuloEntrega findByActiveState(ModuloEntrega  moduloEntrega) {
		return findByActiveStateQuery.runQuery(moduloEntrega);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	//deleteOperation = new DeleteOperation(  getDataSource() );
    	//deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	//findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	//findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	consultaModulosQuery = new ConsultaModulosQuery( getDataSource() );
    	//findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO GES_MODULO_ENTREGA_T ("
				  + " ID, "
				  + " ID_MODULO, "
				  + " ID_PERSONA, "
				  + " ID_SOLICITUD, "
				  + " FECHA_ENTREGA, "
				  + " NRO_CONTRATO_APROV, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
			  	  + " GES_MODULO_ENTREGA_SEC.nextVal, "
				  + PARAMETER
				  + PARAMETER
				  + "(SELECT ID FROM ADM_SOLICITUD_T WHERE RADICADO=?),"
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(ModuloEntrega moduloEntrega) {
			InsertOperation.this.update( new Object[]{
				moduloEntrega.getIdModulo(),
				moduloEntrega.getIdPersona(),
				moduloEntrega.getRadicado(),
				moduloEntrega.getFechaEntrega(),
				moduloEntrega.getNroContratoAprov(),
				moduloEntrega.getEstado(),
				moduloEntrega.getUsuarioCrea()
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE GES_MODULO_ENTREGA_T SET "					  
					  + " ID_MODULO = ? ,"
					  + " ID_PERSONA = ? ,"
					  + " ID_SOLICITUD = (SELECT ID FROM ADM_SOLICITUD_T WHERE RADICADO=?),"
					  + " FECHA_ENTREGA = ? ,"
					  + " NRO_CONTRATO_APROV = ? ,"
					  + " ESTADO = ? ,"
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE"
				  	  + WHERE
				  	  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(ModuloEntrega moduloEntrega) {
			UpdateOperation.this.update( new Object[]{				
				moduloEntrega.getIdModulo(),
				moduloEntrega.getIdPersona(),
				moduloEntrega.getRadicado(),
				moduloEntrega.getFechaEntrega(),
				moduloEntrega.getNroContratoAprov(),
				moduloEntrega.getEstado(),
				moduloEntrega.getUsuarioModifica(),
				moduloEntrega.getId()
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_MODULO_ENTREGA_T "
										+ WHERE + "1=1";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			compile();
		}
		public void execute(ModuloEntrega moduloEntrega) {
			this.update( new Object[]{
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_MODULO_ENTREGA_T "
										+ WHERE + "1=1";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			ModuloEntrega moduloEntrega = new ModuloEntrega();
				moduloEntrega.setId(new Long(rs.getLong("ID")));
				moduloEntrega.setIdModulo(new Long(rs.getLong("ID_MODULO")));
				moduloEntrega.setIdModuloStr(rs.getString("ID_MODULO"));
				moduloEntrega.setIdPersona(new Long(rs.getLong("ID_PERSONA")));
				moduloEntrega.setIdSolicitud(new Long(rs.getLong("ID_SOLICITUD")));
				moduloEntrega.setFechaEntrega(rs.getDate("FECHA_ENTREGA"));		
				moduloEntrega.setNroContratoAprov(rs.getString("NRO_CONTRATO_APROV"));
				moduloEntrega.setEstado(rs.getString("ESTADO"));
				moduloEntrega.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				moduloEntrega.setFechaCrea(rs.getDate("FECHA_CREA"));		
				moduloEntrega.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				moduloEntrega.setFechaModifica(rs.getDate("FECHA_MODIFICA"));
				moduloEntrega.setRadicado(rs.getString("RADICADO"));
				moduloEntrega.setIdTipoIdentificacionStr(rs.getString("TIPO_DTO"));
				moduloEntrega.setIdentificacion(rs.getString("IDENTIFICACION"));
			return moduloEntrega;
		}
    }
	
	abstract class consultaModuloOperation extends MappingSqlQuery {
		public consultaModuloOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			ArrayList typeValueList = new ArrayList();
			String registro[] = {"0","0"};
			registro[0]= rs.getString("ID");
			registro[1]= rs.getString("CODIGO");
			typeValueList.add(registro);
			registro = null;
		return typeValueList;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public ModuloEntrega runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (ModuloEntrega) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID = ? "
			  	  	  					//+ " AND ID_MODULO = ? "
			  	  	  					//+ " AND ID_PERSONA = ? "
			  	  	  					//+ " AND ID_SOLICITUD = ? "
			  	  	  					//+ " AND FECHA_ENTREGA = ? "
			  	  	  					//+ " AND NRO_CONTRATO_APROV = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public ModuloEntrega runQuery(ModuloEntrega moduloEntrega) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//moduloEntrega.getId(),
				//moduloEntrega.getIdModulo(),
				//moduloEntrega.getIdPersona(),
				//moduloEntrega.getIdSolicitud(),
				//moduloEntrega.getFechaEntrega(),
				//moduloEntrega.getNroContratoAprov(),
				//moduloEntrega.getEstado(),
				//moduloEntrega.getUsuarioCrea(),
				//moduloEntrega.getFechaCrea(),
				//moduloEntrega.getUsuarioModifica(),
				//moduloEntrega.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (ModuloEntrega) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = "SELECT MENT.*,RADICADO,ID_TIPO_IDENTIFICACION AS TIPO_DTO, IDENTIFICACION FROM GES_MODULO_ENTREGA_T MENT INNER JOIN "
										+ " ADM_SOLICITUD_T S ON MENT.ID_SOLICITUD = S.ID INNER JOIN "
										+ " ADM_PERSONA_T P ON MENT.ID_PERSONA = P.ID  "										
										+ " WHERE ID_TIPO_IDENTIFICACION = ? AND IDENTIFICACION=?";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.CHAR) );
    		declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public List runQuery(ModuloEntrega moduloEntrega) {
			List result =  execute( new Object[]{
					moduloEntrega.getIdTipoIdentificacionStr(),
					moduloEntrega.getIdentificacion()
			});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }
    
    // ------------- ConsultaModulosQuery ----------------------------------------    
    class ConsultaModulosQuery extends consultaModuloOperation {
		private static final String sql = "SELECT ID,CODIGO FROM GES_MODULO_T";										
    	public ConsultaModulosQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public ModuloEntrega runQuery(ModuloEntrega moduloEntrega) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (ModuloEntrega) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
